package com.lzb.compressmaster.dialog;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lxj.xpopup.core.AttachPopupView;
import com.lzb.compressmaster.R;
import com.lzb.compressmaster.adapter.CommonAdapter;
import com.lzb.compressmaster.bean.CommonBean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class TimeDialog extends AttachPopupView implements View.OnClickListener {

    private RecyclerView rvTimeList;
    private TextView tvCustom, tvStartTime, tvEndTime, tvReset, tvConfirm;
    private CommonAdapter commonAdapter;
    private int selPosition;
    private SelDateDialog startDateDialog, endDateDialog;
    private long startDate, endDate;


    public TimeDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_time;
    }


    @Override
    protected void onCreate() {
        super.onCreate();
        initView();
    }

    protected void initView() {
        tvCustom = findViewById(R.id.tvCustom);
        tvStartTime = findViewById(R.id.tvStartTime);
        tvEndTime = findViewById(R.id.tvEndTime);
        tvReset = findViewById(R.id.tvReset);
        tvConfirm = findViewById(R.id.tvConfirm);

        tvCustom.setOnClickListener(this);
        tvStartTime.setOnClickListener(this);
        tvEndTime.setOnClickListener(this);
        tvReset.setOnClickListener(this);
        tvConfirm.setOnClickListener(this);

        rvTimeList = findViewById(R.id.rvTimeList);
        rvTimeList.setLayoutManager(new LinearLayoutManager(getContext()));
        commonAdapter = new CommonAdapter();
        rvTimeList.setAdapter(commonAdapter);
        commonAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                tvCustom.setTextColor(Color.parseColor("#333333"));
                if (selPosition != 4) commonAdapter.getItem(selPosition).setSelected(false);
                selPosition = position;
                commonAdapter.getItem(position).setSelected(true);
                commonAdapter.notifyDataSetChanged();
                if (listener != null) {
                    listener.clickItem(commonAdapter.getItem(position).getName(), 0, 0);
                }
            }
        });

        List<CommonBean> list = new ArrayList<>();
        list.add(new CommonBean(getContext().getString(R.string.time_text1), true));
        list.add(new CommonBean(getContext().getString(R.string.time_text2), false));
        list.add(new CommonBean(getContext().getString(R.string.time_text3), false));
        list.add(new CommonBean(getContext().getString(R.string.time_text4), false));
        commonAdapter.setList(list);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tvCustom) {//自定义
            if (selPosition != 4) {
                commonAdapter.getItem(selPosition).setSelected(false);
                commonAdapter.notifyItemChanged(selPosition);
            }
            selPosition = 4;
            tvCustom.setTextColor(Color.parseColor("#246FFE"));
        } else if (id == R.id.tvStartTime) {//开始时间
            showSelStartDateDialog();
        } else if (id == R.id.tvEndTime) {//结束时间
            showSelEndDateDialog();
        } else if (id == R.id.tvReset) {//重置
            startDate = 0;
            endDate = 0;
            tvStartTime.setText("");
            tvEndTime.setText("");
        } else if (id == R.id.tvConfirm) {//确定
            if (selPosition == 4) {
                if (startDate == 0) {
                    ToastUtils.showShort(getContext().getString(R.string.please_sel_the_earliest_time));
                    return;
                } else if (endDate == 0) {
                    ToastUtils.showShort(getContext().getString(R.string.please_sel_the_latest_time));
                    return;
                }
                if (listener != null) {
                    listener.clickItem(getContext().getString(R.string.time_text5), 0, 0);
                }
            }
            dismiss();
        }
    }

    private void showSelStartDateDialog() {
        if (startDateDialog == null) {
            startDateDialog = new SelDateDialog(getContext());
        }
        startDateDialog.setListener(new SelDateDialog.Ilistener() {
            @Override
            public void clickConfirm(long date) {
                startDate = date;
                tvStartTime.setText(TimeUtils.millis2String(date, new SimpleDateFormat("yyyy-MM-dd")));
            }
        });
        startDateDialog.show();
    }

    private void showSelEndDateDialog() {
        if (endDateDialog == null) {
            endDateDialog = new SelDateDialog(getContext());
        }
        endDateDialog.setListener(new SelDateDialog.Ilistener() {
            @Override
            public void clickConfirm(long date) {
                endDate = date;
                tvEndTime.setText(TimeUtils.millis2String(date, new SimpleDateFormat("yyyy-MM-dd")));
            }
        });
        endDateDialog.show();
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void clickItem(String name, long start, long end);
    }

}
